import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { PerBusinessFamilyPropertyApi } from '#/api/per/businessFamilyProperty';
import type { DescriptionItemSchema } from '#/components/description';

import { h } from 'vue';

import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';
import { formatDate } from '@vben/utils';

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'relation',
      label: '与本人关系',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.PER_RELATION),
        placeholder: '请选择与本人关系',
      },
    },
    {
      fieldName: 'familyName',
      label: '产权人姓名',
      component: 'Input',
      componentProps: {
        placeholder: '请输入产权人姓名',
      },
    },
    {
      fieldName: 'cardType',
      label: '证件类型',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.SYSTEM_CARD_TYPE),
        placeholder: '请选择证件类型',
      },
    },
    {
      fieldName: 'familyIdcard',
      label: '身份证号',
      component: 'Input',
      componentProps: {
        placeholder: '请输入产权人身份证号',
      },
    },
    {
      fieldName: 'address',
      label: '坐落地址',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        placeholder: '请输入房产坐落地址',
      },
    },
    {
      fieldName: 'buildYear',
      label: '建筑年份',
      component: 'DatePicker',
      componentProps: {
        picker: 'year',
        format: 'YYYY',
        valueFormat: 'YYYY',
      },
    },
    {
      fieldName: 'buildArea',
      label: '建筑面积',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '请输入建筑面积',
        addonAfter: '平方米',
      },
    },
    {
      fieldName: 'propertyType',
      label: '房产性质',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.PER_PROPERTY_TYPE),
        placeholder: '请选择房产性质',
      },
    },
    {
      fieldName: 'transactionDate',
      label: '交易时间',
      component: 'DatePicker',
      componentProps: {
        showTime: false,
        format: 'YYYY-MM-DD',
        valueFormat: 'x',
      },
    },
    {
      fieldName: 'transactionAmount',
      label: '交易金额',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '请输入交易金额',
        addonAfter: '万元',
      },
    },
    {
      fieldName: 'propertyCertNo',
      label: '房产证编号',
      component: 'Input',
      componentProps: {
        placeholder: '请输入房产证编号',
      },
    },
    {
      fieldName: 'purchaseMethod',
      label: '购房方式',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.PER_PURCHASE_METHOD),
        placeholder: '请选择购房方式',
      },
    },
    {
      fieldName: 'isPrimaryResidence',
      label: '是否为主要住宅',
      component: 'RadioGroup',
      defaultValue: false,
      componentProps: {
        options: [
          { label: '是', value: true },
          { label: '否', value: false },
        ],
        buttonStyle: 'solid',
        optionType: 'button',
      },
    },
    {
      fieldName: 'coOwnership',
      label: '共有产权情况',
      component: 'Input',
      componentProps: {
        placeholder: '请输入共有产权情况',
      },
    },
    {
      fieldName: 'propertySource',
      label: '房产来源/去向',
      component: 'Input',
      componentProps: {
        placeholder: '请输入房产来源/去向',
      },
    },
    {
      fieldName: 'fundSource',
      label: '资金来源',
      component: 'Input',
      componentProps: {
        placeholder: '请输入资金来源',
      },
    },
    {
      fieldName: 'remarks',
      label: '备注',
      component: 'Textarea',
      formItemClass: 'col-span-2',
      componentProps: {
        placeholder: '请输入备注',
      },
    },
  ];
}

/** 详情页字段 */
export function useDetailSchema(): DescriptionItemSchema[] {
  return [
    {
      label: '人员ID，对应 per_employee.id',
      field: 'employeeId',
    },
    {
      label: '与本人关系（称谓）',
      field: 'relation',
    },
    {
      label: '系统人员ID，对应system_person.id',
      field: 'systemPersonId',
    },
    {
      label: '产权人姓名',
      field: 'familyName',
    },
    {
      label: '证件类型',
      field: 'cardTypeName',
    },
    {
      label: '产权人身份证号',
      field: 'familyIdcard',
    },
    {
      label: '共有产权情况',
      field: 'coOwnership',
    },
    {
      label: '房产来源/去向',
      field: 'propertySource',
    },
    {
      label: '房产坐落地址',
      field: 'address',
    },
    {
      label: '建筑年份',
      field: 'buildYear',
      render: (val) => formatDate(val, 'YYYY') as string,
    },
    {
      label: '建筑面积（平方米）',
      field: 'buildArea',
    },
    {
      label: '房产性质（住宅/商用/办公等）',
      field: 'propertyTypeName',
    },
    {
      label: '交易时间',
      field: 'transactionDate',
      render: (val) => formatDate(val, 'YYYY-MM-DD') as string,
    },
    {
      label: '资金来源',
      field: 'fundSource',
    },
    {
      label: '交易金额',
      field: 'transactionAmount',
    },
    {
      label: '房产证编号',
      field: 'propertyCertNo',
    },
    {
      label: '购房方式（一次性/贷款/共有）',
      field: 'purchaseMethod',
    },
    {
      label: '是否为主要住宅（1：是，0：否）',
      field: 'isPrimaryResidence',
    },
    {
      label: '备注',
      field: 'remarks',
      render: (val) =>
        h('div', {
          innerHTML: val,
          class: 'whitespace-pre-wrap text-sm leading-relaxed',
        }),
    },
    {
      label: '是否有效（1：有效，0：无效）',
      field: 'statusName',
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'relation',
      label: '与本人关系',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.PER_RELATION),
        placeholder: '请选择与本人关系',
      },
    },
    {
      fieldName: 'familyName',
      label: '产权人姓名',
      component: 'Input',
      componentProps: {
        placeholder: '请输入产权人姓名',
      },
    },
    {
      fieldName: 'cardType',
      label: '证件类型',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.SYSTEM_CARD_TYPE),
        placeholder: '请选择证件类型',
      },
    },
    {
      fieldName: 'familyIdcard',
      label: '身份证号',
      component: 'Input',
      componentProps: {
        placeholder: '请输入产权人身份证号',
      },
    },
    {
      fieldName: 'address',
      label: '坐落地址',
      component: 'Input',
      componentProps: {
        placeholder: '请输入房产坐落地址',
      },
    },
    {
      fieldName: 'propertyType',
      label: '房产性质',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.PER_PROPERTY_TYPE),
        placeholder: '请选择房产性质',
      },
    },
    {
      fieldName: 'propertyCertNo',
      label: '房产证编号',
      component: 'Input',
      componentProps: {
        placeholder: '请输入房产证编号',
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<PerBusinessFamilyPropertyApi.BusinessFamilyProperty>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: '主键ID',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'relation',
      title: '与本人关系',
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.PER_RELATION },
      },
    },
    {
      field: 'familyName',
      title: '产权人姓名',
      minWidth: 120,
    },
    {
      field: 'cardType',
      title: '证件类型',
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.SYSTEM_CARD_TYPE },
      },
    },
    {
      field: 'familyIdcard',
      title: '身份证号',
      minWidth: 120,
    },
    {
      field: 'address',
      title: '房产坐落地址',
      minWidth: 120,
    },
    {
      field: 'buildYear',
      title: '建筑年份',
      minWidth: 120,
    },
    {
      field: 'buildArea',
      title: '建筑面积',
      minWidth: 120,
    },
    {
      field: 'propertyType',
      title: '房产性质',
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.PER_PROPERTY_TYPE },
      },
    },
    {
      field: 'isPrimaryResidence',
      title: '是否主要住宅',
      minWidth: 80,
      cellRender: {
        name: 'CellDictBadge',
      },
    },
    {
      field: 'status',
      title: '状态',
      minWidth: 80,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.COMMON_STATUS },
      },
    },
    {
      field: 'actions',
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
